<?php

/*
system controller:系统参数设置（货品类别，供应商，物料种类，出入库类别，），包括：
用户登录检查
用户权限检查
管理用户，增、删、查、改
*/

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Setting extends CI_Controller {

	public $data;
	public $error;

	public function __construct()
	{
		parent::__construct();
		//载入必要类文件
		$this->load->model('Login_model');
		$this->load->model('User_model');
		$this->load->model('Right_model');
		$this->load->model('Setting_model');
		//载入必要库文件
		$this->load->library('Access_control');
		$this->load->library('Menu_control');
		$this->load->library('Forbid_resubmit');
		//载入配置文件 并 初始化控制器
		$this->data['site'] = $this->config->item('site');
		$this->data['title'] = '系统设置';
		$this->data['styles'] = array('master.css', 'setting.css');
		$this->data['scripts'] = array('master.js', 'setting.js');
		$this->error = FALSE;
		//检查是否是登录用户
		if (!$this->Login_model->check(TRUE))
		{
			header("Location: ".$this->data['site']['root'].'/login');
		}
		//设置站点基本参数
		$this->data['cur_user'] = $this->Login_model->user();
		$this->data['header']['username'] = $this->data['cur_user']['realname'];
		$this->data['site']['environment'] = $this->data['cur_user']['environment'];
		$this->data['menus'] = $this->config->item('menus');
		$this->data['menus'] = $this->menu_control->main($this->data['menus'], 9);
		$this->data['sub_menus'] = $this->_menus();
		$this->data['sub_menu_index'] = '-1';
	}

	private function _menus()
	{
		return array(
					array(
						'index'	=>	1,
						'url'	=>	"{$this->data['site']['root']}setting/basic/",
						'name'	=>	"基础设置"),
					array(
						'index'	=>	2,
						'url'	=>	"{$this->data['site']['root']}setting/advance/",
						'name'	=>	"高级设置"),
					array(
						'index'	=>	0),
					array(
						'index'	=>	12,
						'url'	=>	"{$this->data['site']['root']}setting/scripts/",
						'name'	=>	"自定义脚本")
			);
	}

	public function index()
	{
		$this->basic();
	}

	public function basic()
	{
		if ($this->Setting_model->get_settings())
		{
			$this->data['settings'] = $this->Setting_model->settings();
		}
		else
		{
			$this->error = '系统错误';
			return $this->load->view_('error_500_view', $this->data);
		}
		$this->load->view_('setting_basic_view', $this->data);
	}

	public function advance()
	{

		$this->load->view_('setting_advance_view', $this->data);
	}

	//添加一个入库类型
	public function add_inbound_type()
	{
		if ($this->Setting_model->add_type())
		{
			$this->data['ajax']['request'] = 'DONE';
			$this->data['ajax']['data'] = $this->Setting_model->type_id();
		}
		else
		{
			$this->data['ajax']['request'] = 'FAILED';
			$this->data['ajax']['data'] = $this->Setting_model->error;
		}
		$this->load->view('ajax_json', $this->data);
	}

	//添加一个出库类型
	public function add_outbound_type()
	{
		if ($this->Setting_model->add_type())
		{
			$this->data['ajax']['request'] = 'DONE';
			$this->data['ajax']['data'] = $this->Setting_model->type_id();
		}
		else
		{
			$this->data['ajax']['request'] = 'FAILED';
			$this->data['ajax']['data'] = $this->Setting_model->error;
		}
		$this->load->view('ajax_json', $this->data);
	}

	//自定义脚本
	public function scripts($script_name='')
	{
		$this->data['script_names'] = array('update_stock_status');
		if ('' != $script_name)
		{
			$method = '_script_'.$script_name;
			if (method_exists($this, $method))
			{
				$this->db->trans_begin();
				if ($this->$method())
				{
					$this->data['ajax']['data'] = 'DONE';
					$this->data['ajax']['request'] = 'DONE';
					$this->db->trans_commit();
				}
				else
				{
					$this->data['ajax']['data'] = $this->error;
					$this->data['ajax']['request'] = 'FAILED';
					$this->db->trans_rollback();
				}
			}
			else
			{			
				$this->error = 'SYS_ERR|NO_METHOD';
				$this->data['ajax']['data'] = $this->error;
				$this->data['ajax']['request'] = 'FAILED';
			}
			$this->load->view('ajax_json', $this->data);
			return;
		}
		$this->load->view_('setting_scripts_view', $this->data);
	}

	private function _script_update_stock_status()
	{
		$this->load->model('Stock_model');
		$this->load->model('Stock_Detail_model');
		$this->load->model('Inbound_Type_model');
		$this->load->model('Outbound_Type_model');
		$this->Inbound_Type_model->init();
		$this->Outbound_Type_model->init();
		//获得所有的库存详情单
		$stock_details = $this->Stock_Detail_model->get_detail('', 'ALL', 'SINGLE');
		//开始事务
		set_time_limit(0);
		$this->db->trans_begin();
		//遍历所有库存详情
		foreach ($stock_details as $stock_detail)
		{
			//获得对应的库存单
			$stock = $this->Stock_model->custom_query('get', array('stock_id'=> $stock_detail->stock_id));
			$stock = $stock[0];
			if ('inbound' == $stock->stock_type)
			{
				switch ($stock->stock_bound_type) {
					case 'order':
					case 'return':
					case 'exchange':
					case 'other':
						$flg = $stock->stock_bound_type.'_flg';
						if ($this->Inbound_Type_model->get_by_flg($flg, 'fixed_flg'))
						{
							$bound_types = $this->Inbound_Type_model->inbound_types();
						}
						break;
					default:
						$this->error = 'SYS_ERR';
				}
			}
			else if ('outbound' == $stock->stock_type)
			{
				switch ($stock->stock_bound_type) {
					case 'sale':
					case 'return':
					case 'exchange':
					case 'loss':
					case 'other':
						$flg = $stock->stock_bound_type.'_flg';
						if ($this->Outbound_Type_model->get_by_flg($flg, 'fixed_flg'))
						{
							$bound_types = $this->Outbound_Type_model->outbound_types();
						}
						break;
					default:
						$this->error = 'SYS_ERR';
				}
			}
			else if ('transfer' == $stock->stock_type)
			{
				$bound_types = array(array('id'=>0));
			}

			var_dump($stock->stock_type);

			if ($this->error)
			{
				return FALSE;
			}

			if (empty($bound_types))
			{
				$this->error = 'EMPTY_TYPES';
				return FALSE;
			}

			$data = array(
				'bound_type_id'	=> $bound_types[0]['id']
				);
			$where = array(
				'stock_details_id'	=> $stock_detail->stock_details_id
				);
			if (!$this->Stock_Detail_model->custom_query('update', $where, $data))
			{
				$this->error = 'SQL_ERR : ' . $this->Stock_Detail_model->db->last_query();
				return FALSE;
			}

			$where = array(
				'stock_id'	=> $stock->stock_id
				);
			if (!$this->Stock_model->custom_query('update', $where, $data))
			{
				$this->error = 'SQL_ERR : ' . $this->Stock_model->db->last_query();
				return FALSE;
			}

		}
		return TRUE;
	}

}